/**
 * Created with IntelliJ IDEA.
 * User: Tonis
 * Date: 22.04.12
 * Time: 22:27
 * To change this template use File | Settings | File Templates.
 */
/*
 Native FullScreen JavaScript API
 -------------
 Assumes Mozilla naming conventions instead of W3C for now
 */

(function() {
    window.addEventListener('fullscreeneventchange', function(e) {
        if (document.fullScreen || document.webkitIsFullScreen) {
            log("FULL");
            /* make it look good for fullscreen */
        } else {
            /* return to the normal state in page */
            log("NOT FULL");
        }
    }, true);
    var
        fullScreenApi = {
            supportsFullScreen: false,
            isFullScreen: function() { return false; },
            requestFullScreen: function() {},
            cancelFullScreen: function() {},
            fullScreenEventName: '',
            prefix: ''
        },
        browserPrefixes = 'webkit moz o ms khtml'.split(' ');

    // check for native support
    if (typeof document.cancelFullScreen != 'undefined') {
        fullScreenApi.supportsFullScreen = true;
    } else {
        // check for fullscreen support by vendor prefix
        for (var i = 0, il = browserPrefixes.length; i < il; i++ ) {
            fullScreenApi.prefix = browserPrefixes[i];

            if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined' ) {
                fullScreenApi.supportsFullScreen = true;

                break;
            }
        }
    }

    // update methods to do something useful
    if (fullScreenApi.supportsFullScreen) {
        fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';

        fullScreenApi.isFullScreen = function() {
            switch (this.prefix) {
                case '':
                    return document.fullScreen;
                case 'webkit':
                    return document.webkitIsFullScreen;
                default:
                    return document[this.prefix + 'FullScreen'];
            }
        };
        fullScreenApi.requestFullScreen = function(el) {
            log("REQUEST FULLSCREEN");
            return (this.prefix === '') ? el.requestFullScreen(Element.ALLOW_KEYBOARD_INPUT) : el[this.prefix + 'RequestFullScreen'](Element.ALLOW_KEYBOARD_INPUT);
        };
        fullScreenApi.cancelFullScreen = function(el) {
            log("CANCEL FULLSCREEN");
            return (this.prefix === '') ? document.cancelFullScreen() : document[this.prefix + 'CancelFullScreen']();
        }
    }

    // jQuery plugin
    if (typeof jQuery != 'undefined') {
        jQuery.fn.requestFullScreen = function() {

            return this.each(function() {
                var el = jQuery(this);
                if (fullScreenApi.supportsFullScreen) {
                    fullScreenApi.requestFullScreen(el);
                }
            });
        };
    }

    // export api
    window.fullScreenApi = fullScreenApi;
    document.addEventListener("fullscreenchange", function () {
       if (window.fullScreenApi.isFullScreen()){
           OurGame.settings.screenSize = 'fullscreen';
           markFullScreenSelected();
       }else{
           OurGame.settings.screenSize = 'regular';
           markFullScreenNOTSelected();
       }
    }, false);

    document.addEventListener("mozfullscreenchange", function () {
        log("mozfullscreenchange");
        if (window.fullScreenApi.isFullScreen()){
            log("IS FULL");
            OurGame.settings.screenSize = 'fullscreen';
            markFullScreenSelected();
        }else{
            log("IS NOT FULL");
            OurGame.settings.screenSize = 'regular';
            markFullScreenNOTSelected();
        }
    }, false);

    document.addEventListener("webkitfullscreenchange", function () {
        if (window.fullScreenApi.isFullScreen()){
            OurGame.settings.screenSize = 'fullscreen';
            markFullScreenSelected();
//            $('body').css('background','#FFF');
        }else{
            OurGame.settings.screenSize = 'regular';
            markFullScreenNOTSelected();
//            $('body').css('background',"url('../img/WebBackground.jpg') repeat-x top center, url('../img/Background_col.png') repeat center");
        }
    }, false);
})();